/**
 * @author Martin Stolle
 *
 */
package android.got.tasks;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;

public final class TasksAdder extends Activity {

    static final int DATE_DIALOG_ID = 0;
    String _ID;

    private int _year = -1;
    private int _month = -1;
    private int _day = -1;

    private EditText _titleTask;
    private EditText _taskNote;
    private Button   _pickDate;
    //private Spinner  _taskLists; // TODO get tasklists

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task_details);

        _pickDate  = (Button) findViewById(R.id.DueDatePicker);
        _titleTask = (EditText) findViewById(R.id.editTextTask);
        _taskNote  = (EditText) findViewById(R.id.TaskDescription);
        //_taskLists = (Spinner) findViewById(R.id.spinnerTaskLists);

        _pickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });

        updateDisplay();
    }
    
    // the callback received when the user "sets" the date in the dialog
    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    _year = year;
                    _month = monthOfYear;
                    _day = dayOfMonth;
                    _pickDate.setText( new StringBuilder()
                                         .append(_day).append(".")
                                         .append(_month + 1).append(".")// Month is 0 based so add 1
                                         .append(_year).append(" "));
        }
    };

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            // get the current date
            final Calendar c = Calendar.getInstance();
            _year = c.get(Calendar.YEAR);
            _month = c.get(Calendar.MONTH);
            _day = c.get(Calendar.DAY_OF_MONTH);
            return new DatePickerDialog(this, mDateSetListener, _year, _month, _day);
        }
        return null;
    }

    private void updateDisplay() {
        Intent i = getIntent();
        Bundle b = i.getExtras();

        if (b == null) {
            return;
        }

        String taskTitle = (String) b.get("taskTitle");
        if (taskTitle.length() > 0) {
           _titleTask.setText(taskTitle);

           String taskNote = (String) b.get("taskNote");
           _taskNote.setText(taskNote);

           _ID = (String) b.get("taskID");

           _day = b.getInt("dueDay", -1);
           _month = b.getInt("dueMonth", -1);
           _year = b.getInt("dueYear", -1);
           if (_day != -1 && _day != 0) {
               _pickDate.setText( new StringBuilder()
                                 .append(_day).append(".")
                                 .append(_month + 1).append(".")// Month is 0 based so add 1
                                 .append(_year).append(" "));
           }
        }
    }
    
    public void onSave(View view) {
        String taskTitle = _titleTask.getText().toString();
        if (taskTitle.length() > 0) {
            Intent data = new Intent();
            data.putExtra("taskID", _ID);
            data.putExtra("taskTitle", taskTitle);
            data.putExtra("taskNote", _taskNote.getText().toString());

            data.putExtra("day", _day);
            data.putExtra("month", _month);
            data.putExtra("year", _year);

            setResult(Activity.RESULT_OK, data);
        } else {
            setResult(Activity.RESULT_CANCELED);
        }

        finish();
    }

    public void onCancel(View view) {
        setResult(Activity.RESULT_CANCELED);

        finish();
    }
}